package com.woparker.kiosk.view;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.woparker.kiosk.R;
import com.woparker.kiosk.utils.TimeUtil;

import java.util.ArrayList;

public class TimeKey extends LinearLayout implements View.OnClickListener {
    private static final String TAG = "TimeKey";
    private String day;
    private TextView dayTv;
    private TextView tiemTv;
    private Button day_but;

    private String starttiem;
    private String endtime;

    public View mHeader;
    private Context mContext;
    private ArrayList<TextView> textList = new ArrayList<TextView>();

    public String getStarttiem() {
        return starttiem;
    }

    public void setStarttiem(String starttiem) {
        this.starttiem = starttiem;
    }

    public String getEndtime() {
        return endtime;
    }

    public void setEndtime(String endtime) {
        this.endtime = endtime;
    }

    public TimeKey(Context context, AttributeSet attrs) {
        super(context, attrs);
        mContext = context;
        LayoutInflater inflater = LayoutInflater.from(context);
        mHeader = inflater.inflate(R.layout.view_key_time, this);
        dayTv = mHeader.findViewById(R.id.time_day);
        tiemTv = mHeader.findViewById(R.id.time);
        day_but = mHeader.findViewById(R.id.day_but);
        setViewsClick();
    }


    public void setViewsClick() {
        setViewClick(R.id.upday);
        setViewClick(R.id.nextday);
        setViewClick(R.id.time1);
        setViewClick(R.id.time2);
        setViewClick(R.id.time3);
        setViewClick(R.id.time4);
        setViewClick(R.id.time5);
        setViewClick(R.id.time6);
        setViewClick(R.id.time7);
        setViewClick(R.id.time8);
        setViewClick(R.id.time9);
        setViewClick(R.id.time10);
        setViewClick(R.id.time11);
        setViewClick(R.id.time12);
        setViewClick(R.id.day_but);
    }

    public void plateEditView() {
        info(mHeader);
    }

    private void info(View mHeader) {
        showInfo();
    }

    @Override
    public void onClick(View v) {
        day = dayTv.getText().toString().trim();
        if (v.getId() == R.id.upday) {            //上一天
            upday();
        } else if (v.getId() == R.id.nextday) {   //后一天
            nextday();
        } else if (v.getId() == R.id.time1) {
            settime(0, 2);
        } else if (v.getId() == R.id.time2) {
            settime(2, 4);
        } else if (v.getId() == R.id.time3) {
            settime(4, 6);
        } else if (v.getId() == R.id.time4) {
            settime(6, 8);
        } else if (v.getId() == R.id.time5) {
            settime(8, 10);
        } else if (v.getId() == R.id.time6) {
            settime(10, 12);
        } else if (v.getId() == R.id.time7) {
            settime(12, 14);
        } else if (v.getId() == R.id.time8) {
            settime(14, 16);
        } else if (v.getId() == R.id.time9) {
            settime(16, 18);
        } else if (v.getId() == R.id.time10) {
            settime(18, 20);
        } else if (v.getId() == R.id.time11) {
            settime(20, 22);
        } else if (v.getId() == R.id.time12) {
            settime(22, 24);
        }
    }


    private void setViewClick(int viewId) {
        mHeader.findViewById(viewId).setOnClickListener(this);
    }

    private void nextday() {
        dayTv.setText(TimeUtil.getSpecifiedDayAfter(dayTv.getText().toString().trim(), mContext));
        day = dayTv.getText().toString().trim();
        interception();
    }

    private void upday() {
        dayTv.setText(TimeUtil.getSpecifiedDayBefore(dayTv.getText().toString().trim()));
        day = dayTv.getText().toString().trim();
        interception();
    }


    public void showInfo() {
        dayTv.setText(TimeUtil.timeFormat2(System.currentTimeMillis()));
        day = dayTv.getText().toString().trim();
        tiemTv.setText(TimeUtil.currentTimePeriod());
        interception();
    }


    private void settime(int t1, int t2) {
        showtimrtv(t1, t2);
        starttiem = day + " " + t1 + ":00:00";
        endtime = day + " " + t2 + ":00:00";
        setStarttiem(starttiem);
        setEndtime(endtime);
    }


    private void showtimrtv(int t1, int t2) {
        String str1 = String.valueOf(t1);
        String str2 = String.valueOf(t2);
        if (str1.length() == 1) {
            str1 = "0" + t1;
        }
        if (str2.length() == 1) {
            str2 = "0" + t2;
        }
        tiemTv.setText(str1 + ":00" + "-" + str2 + ":00");
    }

    private void interception() {

        String str1 = tiemTv.getText().toString().trim().substring(0, 2);
        String str2 = tiemTv.getText().toString().trim().substring(6, 8);
        settime(Integer.valueOf(str1), Integer.valueOf(str2));
    }

    public void setSearchListener(OnClickListener onClickListener) {
        day_but.setOnClickListener(onClickListener);
    }
}
